package cn.qingyun.gis.modules.building.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.building.entity.BuildingGaode;
import cn.qingyun.gis.modules.building.entity.BuildingProofreadQueryRequest;
import cn.qingyun.gis.modules.building.entity.BuildingProofreadRequest;
import cn.qingyun.gis.modules.building.service.IBuildingGaodeService;
import cn.qingyun.gis.modules.building.service.IBuildingProofreadAttachmentService;
import cn.qingyun.gis.modules.building.service.IBuildingProofreadService;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: 楼宇摸排高德数据表后台管理
 * @Version: V1.0
 */
@Api(tags = "楼宇摸排高德数据")
@RestController
@RequestMapping("/admin/building")
@Slf4j
public class BuildingGaodeController extends BasicController<BuildingGaode, IBuildingGaodeService> {
    @Resource
    private IBuildingGaodeService buildingGaodeService;
    @Resource
    private IBuildingProofreadAttachmentService attachmentService;
    @Resource
    private IBuildingProofreadService proofreadService;

    @ApiOperation(value = "格式化楼宇勘误数据")
    @GetMapping("/clear")
    public Result<?> clearData() {
        proofreadService.clearData();
        buildingGaodeService.clearData();
        attachmentService.clearData();
        return Result.okData("格式化K12学校勘误数据, 请稍后");
    }

    /**
     * 初始化数据 全量未摸排楼宇数据
     *
     * @param file 全量未摸排楼宇数据
     * @return 返回信息
     */
    @ApiOperation(value = "初始化楼宇勘误数据csv文件")
    @PostMapping("/init")
    public Result<?> initData(@RequestParam("file") MultipartFile file) {
        buildingGaodeService.initData(file);
        return Result.okData("正在导入楼宇勘误数据.csv, 请稍后");
    }


    @GetMapping("/matchGrid")
    @ApiOperation("楼宇-匹配业务规划")
    public Result<?> matchGrid() {
        buildingGaodeService.matchGrid();
        return Result.okData("正在进行楼宇的业务区域匹配");
    }


    @GetMapping("/unProofread")
    @ApiOperation("楼宇未勘误列表查询")
    public Result<?> selectList(@Validated BuildingProofreadQueryRequest request) {
        IPage<BuildingGaode> unProofread = buildingGaodeService.unProofread(request);
        return Result.okData(buildingGaodeService.addConfidence(unProofread));
    }

    @GetMapping("/proofreaded")
    @ApiOperation("楼宇勘误列表查询")
    public Result<?> proofreaded(@Validated BuildingProofreadQueryRequest request) {
        IPage<BuildingGaode> proofreaded = buildingGaodeService.proofreaded(request);
        return Result.okData(buildingGaodeService.addConfidence(proofreaded));
    }

    @GetMapping("/queryProofreadByGId")
    @ApiOperation("楼宇勘误列表查询")
    public Result<?> queryProofreadByGId(BuildingProofreadQueryRequest request) {
        return Result.okData(buildingGaodeService.queryProofreadByGId(request));
    }

    @GetMapping("/queryGatherByGd")
    @ApiOperation("楼宇勘误列表查询")
    public Result<?> queryGatherByGd(BuildingProofreadQueryRequest request) {
        return Result.okData(buildingGaodeService.queryGatherByGd(request));
    }

    @PostMapping("/proofread")
    @ApiOperation("楼宇-摸排")
    public Result<?> proofread(@Validated @RequestBody BuildingProofreadRequest request) {
        return Result.okData(buildingGaodeService.proofread(request));
    }

    /**
     * 分页列表查询
     *
     * @param buildingGaode
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "楼宇摸排高德数据表-分页列表查询")
    @ApiOperation(value = "楼宇摸排高德数据表-分页列表查询", notes = "楼宇摸排高德数据表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BuildingGaode buildingGaode,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BuildingGaode> queryWrapper = QueryGenerator.initQueryWrapper(buildingGaode, req.getParameterMap());
        Page<BuildingGaode> page = new Page<>(pageNo, pageSize);
        IPage<BuildingGaode> pageList = buildingGaodeService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param buildingGaode
     * @return
     */
    @AutoLog(value = "楼宇摸排高德数据表-添加")
    @ApiOperation(value = "楼宇摸排高德数据表-添加", notes = "楼宇摸排高德数据表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BuildingGaode buildingGaode) {
        // 未勘误
        buildingGaode.setKwStatus(0);
        buildingGaode.setCreateBy(ObjectUtil.isEmpty(buildingGaode.getKwPhone()) ? "manual" : buildingGaode.getKwPhone());
        buildingGaodeService.save(buildingGaode);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param buildingGaode
     * @return
     */
    @AutoLog(value = "楼宇摸排高德数据表-编辑")
    @ApiOperation(value = "楼宇摸排高德数据表-编辑", notes = "楼宇摸排高德数据表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody BuildingGaode buildingGaode) {
        buildingGaodeService.updateById(buildingGaode);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼宇摸排高德数据表-通过id删除")
    @ApiOperation(value = "楼宇摸排高德数据表-通过id删除", notes = "楼宇摸排高德数据表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        buildingGaodeService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "楼宇摸排高德数据表-批量删除")
    @ApiOperation(value = "楼宇摸排高德数据表-批量删除", notes = "楼宇摸排高德数据表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.buildingGaodeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼宇摸排高德数据表-通过id查询")
    @ApiOperation(value = "楼宇摸排高德数据表-通过id查询", notes = "楼宇摸排高德数据表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        BuildingGaode buildingGaode = buildingGaodeService.getById(id);
        if (buildingGaode == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(buildingGaode);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param buildingGaode
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BuildingGaode buildingGaode) {
        return super.exportXls(request, buildingGaode, BuildingGaode.class, "楼宇摸排高德数据表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BuildingGaode.class);
    }
}
